/**
 * Created with IntelliJ IDEA.
 * User: Kotone
 * Date: 2016/4/18
 * Time: 13:27
 * To change this template use File | Settings | File Templates.
 */

function request(paramName) {
    //获取URL的字符串
    var sSource = String(window.document.location);
    var sName = paramName;
    var sReturn = "";
    var sQUS = "?";
    var sAMP = "&";
    var sEQ = "=";
    var iPos;

    //获取sSource中的"?"，无则返回 -1
    iPos = sSource.indexOf(sQUS);
    if (iPos == -1)
        return;

    //汲取参数，从iPos letters 置到sSource.length-iPos的 letters 置，
    //若iPos = -1，则：从-1 到 sSource.length+1
    var strQuery = sSource.substr(iPos, sSource.length - iPos);

    // alert(strQuery);
    //先全部转换为小写
    var strLCQuery = strQuery.toLowerCase();
    var strLCName = sName.toLowerCase();

    //从子字符串strLCQuery中查找“?”、参数名，以及“=”，即“?参数名=”
    iPos = strLCQuery.indexOf(sQUS + strLCName + sEQ);
    //alert(iPos);
    //如果不存在
    if (iPos == -1) {
        //继续查找可能的后一个参数，即带“&参数名=”
        iPos = strLCQuery.indexOf(sAMP + strLCName + sEQ);
    }

    //判断是否存在参数
    if (iPos != -1) {
        sReturn = strQuery.substr(iPos + sName.length + 2, strQuery.length - (iPos + sName.length + 2));
        var iPosAMP = sReturn.indexOf(sAMP);
        if (iPosAMP == -1) {
            console.log(sReturn)
            return sReturn.split('#')[0];
        }
        else {
            sReturn = sReturn.substr(0, iPosAMP);
        }
    }
    console.log(sReturn)
    return sReturn.split('#')[0];
}
var buildingId = request('buildingId');
var houseArray = null;
function getBuilding() {
    $('#appointmentHref').attr('href', 'appointment.html?buildingId=' + buildingId);
    $.ajax({
        method: 'GET',
        url: ctx + "/building/getBuilding",
        data: {
            buildingId: buildingId
        },
        async: false,
        success: function (response) {
            var building = response.data;
            //var mapParams='?province='+building.addressResponse.province+
            //    '&city='+building.addressResponse.city+
            //    '&district='+building.addressResponse.district+
            //    '&centerAddress='+building.addressResponse.address+
            //    '&buildingName='+building.name+
            //    '&radius=0';
            var mapParams = '?lat=' + building.addressResponse.lat +
                '&lng=' + building.addressResponse.lng +
                '&buildingName=' + building.name +
                '&radius=0';
            $('#name').html(building.name);
            $('#buildingDescription').html(building.description);
            $('#buildingAppointment').attr('href', 'appointment.html?buildingId=' + buildingId);
            if (null != building.images) {
                var images = building.images.split(',');
                for (var i in images) {
                    var image = images[i];
                    var id = "image" + i;

                    $.ajax({
                        url: ctx + "/photos/getPhoto",
                        data: {
                            key: image,
                            waterMark: true
                        },
                        imageId: id,
                        method: "get",
                        async: false,
                        success: function (response) {
                            //alert(response.responseText)
                            var url = response;
                            //document.getElementById(this.imageId).src = ;
                            $('#images').append('<div class="swiper-slide"><div class="img-box"><img width="100%" src="' + url + "|imageView2/1/w/750/h/423" + '" data-id="' + i + '"></div></div>');
                        },
                        failure: function (response, opts) {

                        }
                    });
                }
            }

            var tags = null != building.tags ? building.tags.split(',') : new Array();
            var tagHtml = '';
            for (var i in tags) {
                tagHtml += '<span class="tip">' + tags[i] + '</span>';
            }
            $('#contentArea').html(
                '<div class="title">' + building.addressResponse.district + '<br>地址：' + building.addressResponse.address + '</div>' +
                '<p>' + tagHtml + '</p>' +
                '<div class="rt-area">' +
                '<div class="price"><span>' + building.averagePrice + '</span></div>' +
                //'<a href="javascript:void(0);" class="bicon bicon-view">全景</a>'+
                '<a href="map.html' + mapParams + '" class="bicon bicon-map">地图</a>' +
                '</div>');
            var floorsNum = "";
            var elevatorNum = "";
            var parkingPositionNum = "";
            if (null != building.floorsNum) {
                floorsNum = building.floorsNum;
            }
            if (null != building.elevatorNum) {
                elevatorNum = building.elevatorNum;
            }
            if (null != building.parkingPositionNum) {
                parkingPositionNum = building.parkingPositionNum;
            }
            $('#contentDetail').append
            ('<li>' +
                '<div class="h-xs-6"><span class="c-gray">物业等级：</span> ' + building.level + '</div>' +
                '<div class="h-xs-6"><span class="c-gray">物业费：</span> ' + building.maintenanceFee + '</div>' +
                '<div class="h-xs-6"><span class="c-gray">层数：</span> ' + floorsNum + '</div>' +
                '<div class="h-xs-6"><span class="c-gray">电梯数量：</span>' + elevatorNum + '</div>' +
                '<div class="h-xs-6"><span class="c-gray">净高：</span>' + building.layerHeight + '</div>' +
                '<div class="h-xs-6"><span class="c-gray">得房率：</span> ' + building.usablePercentage + '</div>' +
                '<div class="h-xs-6"><span class="c-gray">车 letters 数：</span> ' + parkingPositionNum + '</div>' +
                '<div class="h-xs-6"><span class="c-gray">建筑面积：</span> ' + building.totalArea + '</div>' +
                '</li>');
            $.ajax({
                method: 'GET',
                url: ctx + "/houseSource/getHouseSources",
                data: {
                    buildingId: buildingId,
                    param: '',
                    page: 0,
                    limit: 100
                },
                async: false,
                success: function (response) {
                    var houseSources = response.data;
                    houseArray = houseSources;
                    $('#houseNum').html(houseSources.length + '套房源在售');
                    if (houseSources.length > 2) {
                        $('#lastHouseNum').html('查看剩余' + (houseSources.length - 2) + '套房源');
                    } else {
                        $('#lastHouseNum').hide();
                    }
                    for (var i in houseSources) {
                        if (i < 2) {
                            var houseSource = houseSources[i];
                            appendHouse(houseSource, i);
                        }

                    }


                }
            });
        }
    });
}
function showAllHouse() {
    for (var i in houseArray) {
        if (i >= 2) {
            appendHouse(houseArray[i], i);
        }
    }
    $('#lastHouseNum').hide();
}

function appendHouse(houseSource) {
    var imageUrl = "";
    if (null != houseSource.images) {
        $.ajax({
            url: ctx + "/photos/getPhoto",
            data: {
                key: houseSource.images.split(',')[0]
            },
            method: "get",
            async: false,
            success: function (response) {
                //alert(response.responseText)
                var url = response;
                console.log(document.getElementById(this.imageId));
                imageUrl = url + "|imageView2/1/w/200/h/150"
                //$('#images').append('<div class="swiper-slide"><div class="img-box"><img src="'+url + "|imageView2/1/w/320/h/180"+'" data-id="'+i+'"></div></div>');
                //document.getElementById(this.imageId).src = url + "|imageView2/1/w/200/h/150";
            },
            failure: function (response, opts) {

            }
        });
    }
    $('#houseSourceList').append('<li>' +
        '<div class="at-list-main">' +
        '<p>' + houseSource.area + '</p>' +
        '<p>' + houseSource.standardStr + '</p>' +
        '<p>' + houseSource.workplace + '工 letters </p>' +
        '<div class="rt-area">' +
        '<span class="price"><span>' + houseSource.totalPrice + '</span></span><br>' +
        '<a class="am-btn am-btn-primary am-btn-lg" href="appointment.html?houseSourceId=' + houseSource.houseSourceId + '">预约</a>' +
        '</div>' +
        '</div>' +
        '<div class="at-list-thumb">' +
        '<a href="share.html?houseSourceId=' + houseSource.houseSourceId + '"><img src="' + imageUrl + '"></a>' +
        '</div>' +
        '</li>')
}
getBuilding();
//$(document).ready(function () {
//    getBuilding();
//});